import express from 'express'
import { getToken } from '../../auth/auth.js'
import * as userServer from "../../controllers/user.js"

const router = express.Router();


router.post("/",async (req,res)=>{
    
    let username = req.body?.username ?? "";
    let password = req.body?.password ?? "";
    if(username==""||password==""){
        res.send({
            code:400,
            msg:"参数错误"
        })
    }

    let [user] = await userServer.selectUserByUsernameAndPassword(username,password);

    if(user==null){
        res.send({
            code:200,
            msg:"登录失败，用户名或密码错误"
        });
        return;
    }

    res.send({
        code:200,
        msg:"登录成功",
        token:"Bearer " + getToken(user)
    })
})


export default router;